package cn.good.yan.b10;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * b10 时间
 * @author shengren.yan
 * @create 2023-07-29
 */
public class Test {

    public static void main(String[] args) throws Exception{
        Test test = new Test();
//        test.t1();
//        test.t2();
        test.t3();
    }

    /**
     * JDK8之前 时间
     */
    public void t1() throws Exception{
        // 计算耗时时间
        long start = System.currentTimeMillis();
        Thread.sleep(3000);
        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - start));
        // 当前时间
        Date date = new Date();
        // 当前时间戳
        long time = date.getTime();

        // 时间 转 字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String format = sdf.format(new Date());
        // 字符串 转 时间
        Date parse = sdf.parse(format);

        // Calendar 日历类 ,注：月份 从0开始
        Calendar cal = Calendar.getInstance();
        System.out.println("年:" + cal.get(Calendar.YEAR));
        System.out.println("月:" + (cal.get(Calendar.MONTH) + 1));
        System.out.println("日:" + cal.get(Calendar.DAY_OF_MONTH));
        System.out.println("时:" + cal.get(Calendar.HOUR_OF_DAY));
        System.out.println("分:" + cal.get(Calendar.MINUTE));
        System.out.println("秒:" + cal.get(Calendar.SECOND));
        // 也有对应的set方法  设置时间
        // 加时间 add方法
        cal.add(Calendar.MONTH, 3); // + 3月
        // 转成Date
        Date time1 = cal.getTime();
    }

    /**
     * JDK8之后 时间
     */
    public void t2() throws Exception{
        // 当前时间
        LocalDate n1 = LocalDate.now();          // 年月日
        LocalTime n2 = LocalTime.now();         //  时分秒
        LocalDateTime n3 = LocalDateTime.now(); // 年月日 时分秒
        // 设置时间
        LocalDateTime of = LocalDateTime.of(2018, 2, 22, 2, 22, 54);
        System.out.println(of);
        // 获取  年  月  日
        System.out.println(of.getYear());
        System.out.println(of.getMonthValue());
        System.out.println(of.getDayOfMonth());
        // 加上2年
        LocalDateTime of2 = of.plusYears(2);
        System.out.println(of2);
        // 减上2年
        LocalDateTime of3 = of.minusYears(2);
        System.out.println(of3);

        // 获取秒数时间戳（10位）
        long l = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        System.out.println("获取 秒数时间戳：" +l);
        // 获取毫秒数时间戳（13位）
        long l1 = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        System.out.println("获取 毫秒数时间戳：" +l1);

        System.out.println("----------");

        // 当前时间  Instant相当于之前的 Date
        Instant instant = Instant.now();
        System.out.println(instant);
        Date date = new Date();
        System.out.println(date);
        // 北京的时间，差8小时
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
        // 时间戳
        long l2 = instant.toEpochMilli();

        // 对比时间之间间隔
        // 时间比较
        Instant i1 = Instant.now();
        // 延迟加载
        TimeUnit.SECONDS.sleep(1);
        Instant i2 = Instant.now();
        long la = Duration.between(i1, i2).toMillis();
        System.out.println(la);

        // 日期比较
        LocalDate ld =  LocalDate.of(2018,12,11);
        // 延迟加载
        TimeUnit.SECONDS.sleep(1);
        LocalDate ld2 =  LocalDate.now();
        Period between = Period.between(ld, ld2);
        System.out.println(between.getYears());
        System.out.println(between.getMonths());
        System.out.println(between.getDays());
    }

    /**
     * JDK8之后 时间 - 格式化
     */
    public void t3() throws Exception{
        LocalDateTime l = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        // 时间转字符串
        String format = dtf.format(l);
        System.out.println(format);
        // 字符串转时间
        LocalDateTime parse = LocalDateTime.parse(format, dtf);
        System.out.println(parse);

        // Data 与 LocalDateTime转换
        // LocalDate 转为 Date
        LocalDate localDate = LocalDate.now();
        Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        System.out.println("LocalDate 转为 Date: " + date);

        // Date 转为 LocalDateTime
        Date date3 = new Date();
        LocalDateTime localDateTime3 = LocalDateTime.ofInstant(date3.toInstant(), ZoneId.systemDefault());
        System.out.println("Date 转为 LocalDateTime: " + date3);

        // Date 转为 LocalDate
        Date date4 = new Date();
        LocalDateTime localDateTime4 = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        LocalDate localDate4 = localDateTime4.toLocalDate();
        System.out.println("Date 转为 LocalDate: " + localDate4);
    }

    /**
     * JDK8之后 时间 - TemporalAdjuster:时间校验器
     */
    public void t4() throws Exception{
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now); //2020-07-20T19:28:57.822

        //把日期改成10号
        LocalDateTime ldt2 = now.withDayOfMonth(10);
        System.out.println(ldt2);

        //把日期改成下个周末
        LocalDateTime ldt3 = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(ldt3);

        //自定义--获取下一个工作日
        LocalDateTime with = now.with((l) -> {
            LocalDateTime ldt4 = (LocalDateTime) l;
            DayOfWeek dayOfWeek = ldt4.getDayOfWeek();
            //获取今天周几 ，如果是周五 +3天，周六 +2 ，其它 + 1
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                return ldt4.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                return ldt4.plusDays(2);
            } else {
                return ldt4.plusDays(1);
            }
        });
        System.out.println(with);
    }

}
